package com.mall.user.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.IRegisterService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import jodd.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import javax.xml.bind.ValidationException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Component
@Service
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Override
    public UserRegisterResponse register(UserRegisterRequest registerRequest) {
        UserRegisterResponse uresponse = new UserRegisterResponse();

        registerRequest.requestCheck();
    //        2. 向⽤户表中插⼊⼀条记录
        volidUserNameRepeat(registerRequest);
        Member member = new Member();
        member.setUsername(registerRequest.getUserName());
        member.setEmail(registerRequest.getEmail());
        //加密
        String ups = DigestUtils.md5DigestAsHex(registerRequest.getUserPwd().getBytes());
        member.setPassword(ups);
        member.setCreated(new Date());
        member.setUpdated(new Date());
        member.setIsVerified("N");
        member.setState(1);

        int insert = memberMapper.insert(member);
        if(insert!=1)
        {
            uresponse.setCode(SysRetCodeConstants.USER_REGISTER_FAILED.getCode());
            uresponse.setMsg(SysRetCodeConstants.USER_REGISTER_FAILED.getMessage());
            return uresponse;
        }
        //      3. 向⽤户验证表中插⼊⼀条记录
        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(member.getUsername());
        String key = member.getUsername() + member.getPassword() + UUID.randomUUID().toString();
        String uuid = DigestUtils.md5DigestAsHex(key.getBytes());
        userVerify.setUuid(uuid);
        userVerify.setRegisterDate(new Date());
        userVerify.setIsExpire("N");
        userVerify.setIsVerify("N");
        int insert1 = userVerifyMapper.insert(userVerify);
        if(insert1!=1)
        {
            uresponse.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getCode());
            uresponse.setMsg(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getMessage());
            return uresponse;
        }
        //    4. 发送⽤户激活邮件
        log.info("注册成功:{}",member);
        uresponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        uresponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return uresponse;
    }

    private void volidUserNameRepeat(UserRegisterRequest registerRequest) {
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username",registerRequest.getUserName());
        List<Member> members = memberMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(members))
        {
            throw new ValidateException(SysRetCodeConstants.USERNAME_ALREADY_EXISTS.getCode(),SysRetCodeConstants.USERNAME_ALREADY_EXISTS.getMessage());
        }
    }
}
